package com.wash.car.biz.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pmo.boot.common.BizException;
import com.wash.car.biz.StoreServer;
import com.wash.car.dao.domain.AbBaseDomain;
import com.wash.car.dao.domain.CarStoreDomain;
import com.wash.car.dao.mapper.CarStoreMapper;
import com.wash.car.dao.model.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class StoreServerImpl implements StoreServer {
    @Resource
    CarStoreMapper carStoreMapper;

    @Override
    public void add(StoreAddModel model) {
        CarStoreDomain dbDomain = carStoreMapper.selectOne(new CarStoreDomain().setName(model.getName()));
        if (ObjectUtil.isNotEmpty(dbDomain)) {
            throw BizException.error("9995", "门店名称已经存在！");
        }
        CarStoreDomain addStoreDomain = AbBaseDomain.setDefaultValue(CarStoreDomain.class);
        BeanUtil.copyProperties(model, addStoreDomain);
        carStoreMapper.insertSelective(addStoreDomain);
    }

    @Override
    public void edit(StoreEditModel model) {
        if(model.getId() == null) {
            throw BizException.error("9995", "门店编号缺失！");
        }
        CarStoreDomain editStoreDomain = new CarStoreDomain();
        BeanUtil.copyProperties(model, editStoreDomain);
        editStoreDomain.setUpdateTime(new Date());
        editStoreDomain.setUpdateUser("sys");
        carStoreMapper.updateByPrimaryKeySelective(editStoreDomain);
    }

    @Override
    public void del(StoreDeleteModel model) {
        carStoreMapper.deleteByPrimaryKey(model.getId());
    }


    @Override
    public StoreDetailsModel findStore(StoreWhereModel model) {
        CarStoreDomain domain = new CarStoreDomain();
        domain.setLevel(model.getLevel()).setId(model.getStoreId());
        CarStoreDomain dbDomain = carStoreMapper.selectOne(domain);
        return BeanUtil.copyProperties(dbDomain, StoreDetailsModel.class);
    }


    @Override
    public PageInfo<StorePageModel> page(StorePageWhereModel model) {
        PageHelper.startPage(model.getPageNum(), model.getPageSize(), model.getOrderBySql());
        List<StorePageModel> list = carStoreMapper.selectPage(BeanUtil.beanToMap(model));
        PageInfo<StorePageModel> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }


}
